# SPDX-License-Identifier: GPL-2.0-only

# Man pages are only built if they are enabled at configure time.
#
# They should always be built before creating a distribution tarball.

# function which adds the source directory prefix and adds a given suffix
manaddsuffix = $(addsuffix $(1),$(addprefix $(srcdir)/,$(2)))

# Local targets.
LOCAL =

# List only the names without the .*.txt extension here:
MAN1_NAMES = \
	lttng \
	lttng-create \
	lttng-destroy \
	lttng-set-session \
	lttng-save \
	lttng-load \
	lttng-start \
	lttng-stop \
	lttng-version \
	lttng-view \
	lttng-enable-channel \
	lttng-disable-channel \
	lttng-add-context \
	lttng-list \
	lttng-track \
	lttng-untrack \
	lttng-status \
	lttng-help \
	lttng-snapshot \
	lttng-enable-event \
	lttng-disable-event \
	lttng-crash \
	lttng-metadata \
	lttng-regenerate \
	lttng-rotate \
	lttng-enable-rotation \
	lttng-disable-rotation \
	lttng-clear \
	lttng-add-trigger \
	lttng-remove-trigger \
	lttng-list-triggers
MAN3_NAMES =
MAN7_NAMES = lttng-event-rule lttng-concepts
MAN8_NAMES = lttng-sessiond lttng-relayd
MAN1_NO_ASCIIDOC_NAMES =
MAN3_NO_ASCIIDOC_NAMES = lttng-health-check
MAN7_NO_ASCIIDOC_NAMES =
MAN8_NO_ASCIIDOC_NAMES =

# AsciiDoc sources and outputs
MAN1_TXT = $(call manaddsuffix,.1.txt,$(MAN1_NAMES))
MAN3_TXT = $(call manaddsuffix,.3.txt,$(MAN3_NAMES))
MAN7_TXT = $(call manaddsuffix,.7.txt,$(MAN7_NAMES))
MAN8_TXT = $(call manaddsuffix,.8.txt,$(MAN8_NAMES))
MAN_TXT = $(MAN1_TXT) $(MAN3_TXT) $(MAN7_TXT) $(MAN8_TXT)
MAN_XML = $(patsubst $(srcdir)/%.txt,%.xml,$(MAN_TXT))

# common AsciiDoc source files
COMMON_TXT = \
	$(srcdir)/common-footer.txt \
	$(srcdir)/common-lttng-cmd-after-options.txt \
	$(srcdir)/common-lttng-cmd-options-head.txt \
	$(srcdir)/common-lttng-cmd-help-options.txt \
	$(srcdir)/common-help-option.txt \
	$(srcdir)/common-intro.txt \
	$(srcdir)/common-daemon-cfg.txt

# config
ASCIIDOC_CONF = $(srcdir)/asciidoc.conf
ASCIIDOC_ATTRS_CONF = $(builddir)/asciidoc-attrs.conf
XSL_FILE = $(srcdir)/manpage.xsl

# common dependencies
COMMON_DEPS = $(ASCIIDOC_CONF) $(COMMON_TXT)

# man pages destinations
MAN1 = $(addsuffix .1,$(MAN1_NAMES))
MAN3 = $(addsuffix .3,$(MAN3_NAMES))
MAN7 = $(addsuffix .7,$(MAN7_NAMES))
MAN8 = $(addsuffix .8,$(MAN8_NAMES))
MAN1_NO_ASCIIDOC = $(addsuffix .1,$(MAN1_NO_ASCIIDOC_NAMES))
MAN3_NO_ASCIIDOC = $(addsuffix .3,$(MAN3_NO_ASCIIDOC_NAMES))
MAN7_NO_ASCIIDOC = $(addsuffix .7,$(MAN7_NO_ASCIIDOC_NAMES))
MAN8_NO_ASCIIDOC = $(addsuffix .8,$(MAN8_NO_ASCIIDOC_NAMES))
MAN = $(MAN1) $(MAN3) $(MAN7) $(MAN8)

# initially empty
CLEANFILES =

if EMBED_HELP
mantoh_verbose = $(mantoh_verbose_@AM_V@)
mantoh_verbose_ = $(mantoh_verbose_@AM_DEFAULT_V@)
mantoh_verbose_0 = @echo "  MANTOH    " $@;

MAN1_H = $(addsuffix .1.h,$(MAN1_NAMES))
MAN3_H = $(addsuffix .3.h,$(MAN3_NAMES))
MAN8_H = $(addsuffix .8.h,$(MAN8_NAMES))
MAN_H = $(MAN1_H) $(MAN3_H) $(MAN8_H)
MAN_H_RECIPE = \
	$(mantoh_verbose_0)\
	MANWIDTH=80 @MANPROG@ --encoding=UTF-8 --no-hyphenation --no-justification --local-file $< > $@ ; \
	$(SED) -i 's/\\/\\\\/g' $@ ; \
	$(SED) -i 's/"/\\"/g' $@ ; \
	$(SED) -i 's/^\(.*\)$$/"\1\\n"/' $@

%.1.h: %.1
	$(MAN_H_RECIPE)

%.3.h: %.3
	$(MAN_H_RECIPE)

%.8.h: %.8
	$(MAN_H_RECIPE)

LOCAL += $(MAN_H)

CLEANFILES += $(MAN_H)
endif # EMBED_HELP

if MAN_PAGES_OPT
# at this point, we know the user asked to build the man pages
if HAVE_ASCIIDOC_XMLTO

asciidoc_verbose = $(asciidoc_verbose_@AM_V@)
asciidoc_verbose_ = $(asciidoc_verbose_@AM_DEFAULT_V@)
asciidoc_verbose_0 = @echo "  ASCIIDOC  " $@;

xmlto_verbose = $(xmlto_verbose_@AM_V@)
xmlto_verbose_ = $(xmlto_verbose_@AM_DEFAULT_V@)
xmlto_verbose_0 = @echo "  XMLTO     " $@;

# tools
ADOC = $(asciidoc_verbose)$(ASCIIDOC) -f $(ASCIIDOC_CONF) -f $(ASCIIDOC_ATTRS_CONF) -d manpage
ADOC_DOCBOOK = $(ADOC) -b docbook
XTO = $(xmlto_verbose)$(XMLTO) -m $(XSL_FILE) man

# only add this dependency if we can build the man pages because it's
# a file generated by the configure script, so it's more recent than
# the pregenerated man pages in a tarball
COMMON_DEPS += $(ASCIIDOC_ATTRS_CONF)

# recipes
%.1.xml: $(srcdir)/%.1.txt $(COMMON_DEPS)
	$(ADOC_DOCBOOK) -o $@ $<

%.1: %.1.xml $(XSL_FILE)
	$(XTO) $< 2>/dev/null

%.3.xml: $(srcdir)/%.3.txt $(COMMON_DEPS)
	$(ADOC_DOCBOOK) -o $@ $<

%.3: %.3.xml $(XSL_FILE)
	$(XTO) $< 2>/dev/null

%.7.xml: $(srcdir)/%.7.txt $(COMMON_DEPS)
	$(ADOC_DOCBOOK) -o $@ $<

%.7: %.7.xml $(XSL_FILE)
	$(XTO) $< 2>/dev/null

%.8.xml: $(srcdir)/%.8.txt $(COMMON_DEPS)
	$(ADOC_DOCBOOK) -o $@ $<

%.8: %.8.xml $(XSL_FILE)
	$(XTO) $< 2>/dev/null

# only clean the generated files if we have the tools to generate them again
CLEANFILES += $(MAN_XML) $(MAN)
else # HAVE_ASCIIDOC_XMLTO
# create man page targets used to stop the build if we want to
# build the man pages, but we don't have the necessary tools to do so
ERR_MSG = "Error: Cannot build target because asciidoc or xmlto tool is missing."
ERR_MSG += "Make sure both tools are installed and run the configure script again."

%.1: $(srcdir)/%.1.txt $(COMMON_DEPS)
	@echo $(ERR_MSG)
	@false

%.3: $(srcdir)/%.3.txt $(COMMON_DEPS)
	@echo $(ERR_MSG)
	@false

%.7: $(srcdir)/%.7.txt $(COMMON_DEPS)
	@echo $(ERR_MSG)
	@false

%.8: $(srcdir)/%.8.txt $(COMMON_DEPS)
	@echo $(ERR_MSG)
	@false
endif # HAVE_ASCIIDOC_XMLTO
endif # MAN_PAGES_OPT

# those are always installed since they are directly written in troff
dist_man1_MANS = $(MAN1_NO_ASCIIDOC)
dist_man3_MANS = $(MAN3_NO_ASCIIDOC)
dist_man7_MANS = $(MAN7_NO_ASCIIDOC)
dist_man8_MANS = $(MAN8_NO_ASCIIDOC)

if MAN_PAGES_OPT
# building man pages: we can install and distribute them
dist_man1_MANS += $(MAN1)
dist_man3_MANS += $(MAN3)
dist_man7_MANS += $(MAN7)
dist_man8_MANS += $(MAN8)
endif # MAN_PAGES_OPT

MAN_HIERARCHY = man1 man3 man7 man8

LOCAL += $(MAN_HIERARCHY)

CLEANFILES += $(MAN_HIERARCHY)

# Mimic mandb(8) path hierarchy -- as if installed -- by symlinking
# sub-directories to the top man directory. This allows developers to read
# the locally generated man pages. For example `$ ./pre-inst-env man lttng'.
man1:
	@ln -s $(abs_builddir) $@

man3:
	@ln -s $(abs_builddir) $@

man7:
	@ln -s $(abs_builddir) $@

man8:
	@ln -s $(abs_builddir) $@

if !MAN_PAGES_OPT
dist-hook:
	@echo "Error: Please enable the man pages before creating a distribution tarball."
	@false
endif # !MAN_PAGES_OPT

# always distribute the source files
EXTRA_DIST = $(MAN_TXT) $(COMMON_TXT) $(XSL_FILE) \
	$(ASCIIDOC_CONF) $(ASCIIDOC_ATTRS_CONF).in

# Build local targets.
all-local: $(LOCAL)

# keep generated man pages that can be considered intermediate files
.PRECIOUS: %.1 %.3 %.7 %.8
